<?php

/**
 * droitsUtilisateur actions.
 *
 * @package    appcemhti
 * @subpackage droitsUtilisateur
 * @author     Steve Cancès
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class droitsUtilisateurActions extends sfActions
{
    /**
    * Executes index action
    *
    * @param sfRequest $request A request object
    */
    public function executeIndex(sfWebRequest $request)
    {

    }
    
    public function executeShowLot(sfWebRequest $request)
    {
        $this->forward404Unless($this->lot = Doctrine_Core::getTable('Lot')->find(array($request->getParameter('id_lot'))), sprintf('Object lot does not exist (%s).', $request->getParameter('id_lot')));
        
    }

    
    public function executeModifDroit(sfWebRequest $request)
    {
        if ($request->isXmlHttpRequest())
        {
            $this->forward404Unless($this->user = Doctrine_Core::getTable('User')->find(array($request->getParameter('id_user'))), sprintf('Object user does not exist (%s).', $request->getParameter('id_user')));
            $this->forward404Unless($this->lot = Doctrine_Core::getTable('Lot')->find(array($request->getParameter('id_lot'))), sprintf('Object lot does not exist (%s).', $request->getParameter('id_lot')));
            $this->forward404Unless($this->typeDroit = Doctrine_Core::getTable('TypeDroit')->find(array($request->getParameter('id_type_droit'))), sprintf('Object type_droit does not exist (%s).', $request->getParameter('id_type_droit')));
            $this->droitsUtilisateur = Doctrine_Core::getTable('DroitsUtilisateur')->getDroitsUtilisateurParLotUser($this->user->getIdUser(), $this->lot->getIdLot());
            // Si aucun droit n'existe
            if($this->droitsUtilisateur == null)
            {
                // On en créer un
                $this->droitsUtilisateur = new DroitsUtilisateur();
                $this->droitsUtilisateur->setLot($this->lot);
                $this->droitsUtilisateur->setUser($this->user);
            }
            $this->droitsUtilisateur->setTypeDroit($this->typeDroit);
            //Le droit est spécifique
            $this->droitsUtilisateur->setDroitSpecifique(1);
            $this->droitsUtilisateur->save();
            
            return true;
        }
    }
    
    public function executeDeleteDroit(sfWebRequest $request)
    {
        //if ($request->isXmlHttpRequest())
        //{
            $this->forward404Unless($this->user = Doctrine_Core::getTable('User')->find(array($request->getParameter('id_user'))), sprintf('Object user does not exist (%s).', $request->getParameter('id_user')));
            $this->forward404Unless($this->lot = Doctrine_Core::getTable('Lot')->find(array($request->getParameter('id_lot'))), sprintf('Object lot does not exist (%s).', $request->getParameter('id_lot')));
            $this->droitsUtilisateur = Doctrine_Core::getTable('DroitsUtilisateur')->getDroitsUtilisateurParLotUser($this->user->getIdUser(), $this->lot->getIdLot());
            // Si aucun droit n'existe
            if($this->droitsUtilisateur == null)
            {
                // On en créer un
                $this->droitsUtilisateur = new DroitsUtilisateur();
                $this->droitsUtilisateur->setLot($this->lot);
                $this->droitsUtilisateur->setUser($this->user);
            }
            //Le droit n'est plus spécifique
            $this->droitsUtilisateur->setDroitSpecifique(null);
            $this->droitsUtilisateur->save();
            //On le met à jour
            $this->droitsUtilisateur->MAJDroit();
            
            return true;
        //}
    }
    
    
    public function executeListDroitsExistants(sfWebRequest $request)
    {
        if ($request->isXmlHttpRequest())
        {
            $sSearch = $request->getParameter("sSearch");
            $iSortCol_0 = $request->getParameter("iSortCol_0");
            $sSortDir_0 = $request->getParameter("sSortDir_0");
            $iDisplayStart = $request->getParameter("iDisplayStart");
            $iDisplayLength = $request->getParameter("iDisplayLength");
            $page = number_format($iDisplayStart / $iDisplayLength) + 1;
            
            
            $q = Doctrine_Core::getTable('DroitsUtilisateur')->getDroitsUtilisateurExistantsDatatable($sSearch, $iSortCol_0, $sSortDir_0);

            $this->pager = new sfDoctrinePager(
                'user',
                sfConfig::get(2)
            );
            $this->pager->setQuery($q);
            $this->pager->setMaxPerPage($iDisplayLength);
            $this->pager->setMaxRecordLimit($iDisplayLength);
            $this->pager->setPage($page);
            $this->pager->init();
            
            $pager = $this->pager;

            $this->getContext()->getConfiguration()->loadHelpers('Url'); 
            
            $aaData = array();
            $list = $pager->getResults();
            foreach ($list as $v)
            {
                $droitDef = $v->getUser()->droitDefault($v->getIdLot());
                $droitSpec = $v->getTypeDroit();
                
                
                $sfGuardU = Doctrine_Core::getTable('sfGuardUser')->find(array($v->getIdUser()));
                
                if($droitSpec != null) $libelleDroitSpec = $droitSpec->getLibelle(); else $libelleDroitSpec = "";
                
                $aaData[] = array(
                    "0" => $v->getIdLot(),
                    "1" => $v->getLot()->getNom(),
                    "2" => $v->getIdUser(),
                    "3" => $sfGuardU->getFirstName()." ".$sfGuardU->getLastName(),
                    "4" => '<s>'.$droitDef->getLibelle().'</s>',
                    "5" => $droitSpec->getLibelle()
                );
            }

            $output = array(
            "iTotalRecords" => count($pager),
            "iTotalDisplayRecords" => count($pager),
            "aaData" => $aaData,
            );

            return $this->renderText(json_encode($output));
        }
    }
    
    
    public function executeListUtilisateur(sfWebRequest $request)
    {
        if ($request->isXmlHttpRequest())
        {
            $sSearch = $request->getParameter("sSearch");
            $iSortCol_0 = $request->getParameter("iSortCol_0");
            $sSortDir_0 = $request->getParameter("sSortDir_0");
            $iDisplayStart = $request->getParameter("iDisplayStart");
            $iDisplayLength = $request->getParameter("iDisplayLength");
            $page = number_format($iDisplayStart / $iDisplayLength) + 1;
            
            
            $q = Doctrine_Core::getTable('sfGuardUser')->getSfGuardUserDatatable($sSearch, $iSortCol_0, $sSortDir_0);

            $this->pager = new sfDoctrinePager(
                'user',
                sfConfig::get(2)
            );
            $this->pager->setQuery($q);
            $this->pager->setMaxPerPage($iDisplayLength);
            $this->pager->setMaxRecordLimit($iDisplayLength);
            $this->pager->setPage($page);
            $this->pager->init();
            
            $pager = $this->pager;

            //$pager = $this->getPager('table', $q, $request->getParameter('page', 10), $request->getParameter('iDisplayLength'));

            $this->getContext()->getConfiguration()->loadHelpers('Url'); 
            
            $aaData = array();
            $list = $pager->getResults();
            foreach ($list as $v)
            {                    
                if($v->isAdmin() == 1) $rang = "Administrateur"; else $rang = "Utilisateur";
                if($v->getIsActive() == 1) $actif = "Oui"; else $actif = "Non";

                $consulter = '<a class="submit" href="' . url_for('user/show?id='. $v->getId()) . '">Consulter</a>';       
                $actions = "";
                $actions = $actions . $consulter;

                
                $aaData[] = array(
                    "0" => $v->getId(),
                    "1" => $v->getFirstName(),
                    "2" => $v->getLastName(),
                    "3" => "<a href=mailto:" . $v->getEmailAddress() . ">" . $v->getEmailAddress() . "</a>",
                    "4" => $v->getUsername(),
                    "5" => $actif,
                    "6" => $rang,
                    "7" => $actions,
                );
            }

            $output = array(
            "iTotalRecords" => count($pager),
            "iTotalDisplayRecords" => count($pager),
            "aaData" => $aaData,
            );

            return $this->renderText(json_encode($output));
        }
    }
    
    public function executeListLot(sfWebRequest $request)
    {
        if ($request->isXmlHttpRequest())
        {
            $sSearch = $request->getParameter("sSearch");
            $iSortCol_0 = $request->getParameter("iSortCol_0");
            $sSortDir_0 = $request->getParameter("sSortDir_0");
            $iDisplayStart = $request->getParameter("iDisplayStart");
            $iDisplayLength = $request->getParameter("iDisplayLength");
            $page = number_format($iDisplayStart / $iDisplayLength) + 1;
            
            
            $q = Doctrine_Core::getTable('lot')->getLotDatatable($sSearch, $iSortCol_0, $sSortDir_0);
            
            
            $this->pager = new sfDoctrinePager(
                'lot',
                sfConfig::get(2)
            );
            $this->pager->setQuery($q);
            $this->pager->setMaxPerPage($iDisplayLength);
            $this->pager->setMaxRecordLimit($iDisplayLength);
            $this->pager->setPage($page);
            $this->pager->init();
            
            $pager = $this->pager;

            $this->getContext()->getConfiguration()->loadHelpers('Url', 'Date');
            $aaData = array();
            $list = $pager->getResults();
           
            
            foreach ($list as $v)
            {
                $actions = "";
                $consulter = '<a class="submit" href="' . url_for('droitsUtilisateur/showLot?id_lot='. $v->getIdLot()) . '">Consulter</a>';
                //$passerConfidentiel = '<a class="submit passerConfidentiel" id="passerConfidentiel_'. $v->getIdLot() . '">passer confidentiel</a>';
                //$passerNonConfidentiel = '<a class="submit passerNonConfidentiel" id="passerNonConfidentiel_'. $v->getIdLot() . '">passer non confidentiel</a>';
                
                $actions = $consulter;
                
                if($v->getConfidentiel() == 1)
                {
                    $conf = "Oui";
                    //$actions = $actions.$passerNonConfidentiel;
                }else
                {
                    $conf = "Non";
                    //$actions = $actions.$passerConfidentiel;
                }
                $fournisseur = "";
                if($v->getIdFournisseur()) $fournisseur = $v->getFournisseur()->getNom();
                $user = Doctrine_Core::getTable('sfGuardUser')->find(array($v->getUser()->getIdUser()));
                $aaData[] = array(
                    "0" => $v->getIdLot(),
                    "1" => $v->getNom(),
                    "2" => $fournisseur,
                    "3" => $v->getDateTimeObject('date_recep')->format('d/m/Y'),
                    "4" => $v->getEchantillon()->count(),
                    "5" => $v->getMatiere()->getLibelle(),
                    "6" => $conf,
                    "7" => $user->getFirstName() . " " . $user->getLastName(),
                    "8" => $actions,
                );
            }

            $output = array(
            "iTotalRecords" => count($pager),
            "iTotalDisplayRecords" => count($pager),
            "aaData" => $aaData,
            );

            return $this->renderText(json_encode($output));
        }
    }
    
    protected function responseJSON($status, $msg)
    {
        $response = array(
            "status" => $status,
            "msg" => $msg
        );
        return $this->renderText(json_encode($response));
    }
    
    public function executePasserLotConfidentiel(sfWebRequest $request)
    {
        if ($request->isXmlHttpRequest())
        {
            $idLot = intval($request->getParameter("id_lot"));
            $lot = Doctrine_Core::getTable('Lot')->find(array($idLot));
            if($lot != null)
            {
                $lot->setConfidentiel(1);
                $lot->save();
                $lot->calculerDroitUtilisateurs();
                return $this->responseJSON("success", "Le lot ".$lot->getIdLot()." : ".$lot->getNom()." est maintenant confidentiel");
            }
            else return false;
        }
    }
    
    public function executePasserLotNonConfidentiel(sfWebRequest $request)
    {
        if ($request->isXmlHttpRequest())
        {
            $idLot = intval($request->getParameter("id_lot"));
            $lot = Doctrine_Core::getTable('Lot')->find(array($idLot));
            if($lot != null)
            {
                $lot->setConfidentiel(null);
                $lot->save();
                $lot->calculerDroitUtilisateurs();
                return $this->responseJSON("success", "Le lot ".$lot->getIdLot()." : ".$lot->getNom()." n'est maintenant plus confidentiel");
            }
            else return false;
        }
    }
    
    public function executeListDroitsExistantsParLot(sfWebRequest $request)
    {
        if ($request->isXmlHttpRequest())
        {
            $idLot= intval($request->getParameter("idLot"));
            $sSearch = $request->getParameter("sSearch");
            $iSortCol_0 = $request->getParameter("iSortCol_0");
            $sSortDir_0 = $request->getParameter("sSortDir_0");
            $iDisplayStart = $request->getParameter("iDisplayStart");
            $iDisplayLength = $request->getParameter("iDisplayLength");
            $page = number_format($iDisplayStart / $iDisplayLength) + 1;
            
            $lot = Doctrine_Core::getTable('Lot')->find(array($idLot));
            
            $q = Doctrine_Core::getTable('DroitsUtilisateur')->getDroitsUtilisateurExistantsParLotDatatable($idLot, $sSearch, $iSortCol_0, $sSortDir_0);

            $this->pager = new sfDoctrinePager(
                'DroitsUtilisateur',
                sfConfig::get(2)
            );
            $this->pager->setQuery($q);
            $this->pager->setMaxPerPage($iDisplayLength);
            $this->pager->setMaxRecordLimit($iDisplayLength);
            $this->pager->setPage($page);
            $this->pager->init();
            
            $pager = $this->pager;

            $this->getContext()->getConfiguration()->loadHelpers('Url'); 
            
            $aaData = array();
            $list = $pager->getResults();
            foreach ($list as $v)
            {
                $user = $v->getUser();
                $droitDef = $user->droitDefault($lot->getIdLot());
                $droitSpec = null;
                if($v->getIdLot() == $idLot) $droitSpec = $v->getTypeDroit();
                
                
                $sfGuardU = Doctrine_Core::getTable('sfGuardUser')->find(array($v->getIdUser()));
                
                if($droitSpec != null) $libelleDroitSpec = $droitSpec->getLibelle(); else $libelleDroitSpec = "";
                $aaData[] = array(
                    "0" => $v->getIdUser(),
                    "1" => $sfGuardU->getFirstName()." ".$sfGuardU->getLastName(),
                    "2" => '<s>'.$droitDef->getLibelle().'</s>',
                    "3" => $libelleDroitSpec,
                );
            }

            $output = array(
            "iTotalRecords" => count($pager),
            "iTotalDisplayRecords" => count($pager),
            "aaData" => $aaData,
            );

            return $this->renderText(json_encode($output));
        }
    }
    
    public function executeListUtilisateurShowLot(sfWebRequest $request)
    {
        if ($request->isXmlHttpRequest())
        {
            $idLot= intval($request->getParameter("idLot"));
            $sSearch = $request->getParameter("sSearch");
            $iSortCol_0 = $request->getParameter("iSortCol_0");
            $sSortDir_0 = $request->getParameter("sSortDir_0");
            $iDisplayStart = $request->getParameter("iDisplayStart");
            $iDisplayLength = $request->getParameter("iDisplayLength");
            $page = number_format($iDisplayStart / $iDisplayLength) + 1;
            
            $lot = Doctrine_Core::getTable('Lot')->find(array($idLot));
            
            
            $q = Doctrine_Core::getTable('User')->getUserDroitDatatable($sSearch, $iSortCol_0, $sSortDir_0);
            
            
            $this->pager = new sfDoctrinePager(
                'User',
                sfConfig::get(2)
            );
            $this->pager->setQuery($q);
            $this->pager->setMaxPerPage($iDisplayLength);
            $this->pager->setMaxRecordLimit($iDisplayLength);
            $this->pager->setPage($page);
            $this->pager->init();
            
            $pager = $this->pager;

            $this->getContext()->getConfiguration()->loadHelpers('Url', 'Date');
            $aaData = array();
            $list = $pager->getResults();
           
            
            foreach ($list as $v)
            {
                $droitDef = $v->droitDefault($lot->getIdLot());
                $droits = $v->getDroitsUtilisateur();
                $droit = null;
                foreach ($droits as $droitCourant)
                {
                    if($droitCourant->getIdLot() == $idLot) $droit = $droitCourant;
                }
                
                $actions = "";
                $niLectureNiEcriture = '<a id="modifDroit_'.$v->getIdUser().'_1" class="submit modifDroit" href="">Ni lecture ni écriture</a>';
                $lectureSeulement = '<a id="modifDroit_'.$v->getIdUser().'_2" class="submit modifDroit" href="">lecture seulement</a>';
                $lectureEtEcriture = '<a id="modifDroit_'.$v->getIdUser().'_3" class="submit modifDroit" href="">lecture et écriture</a>';
                $droitDefaut = '<a id="deleteDroit_'.$v->getIdUser().'" class="submit deleteDroit" href="">droit par défaut</a>';
                $actions = $niLectureNiEcriture.$lectureSeulement.$lectureEtEcriture.$droitDefaut;
                
                //$consulter = '<a class="submit" href="lot/show/id_lot/' . $v->getIdLot() . '">Consulter</a>';
                if($lot->getConfidentiel() == 1) $conf = "Oui"; else $conf = "Non";
                $userGuard = Doctrine_Core::getTable('sfGuardUser')->find(array($v->getIdUser()));
                
                if($droit == null)
                {
                    $libelleDroitDef = $droitDef->getLibelle();
                    $libelleDroitSpec = "";
                }
                else
                {
                    if($droit->getDroitSpecifique() == 1)
                    {
                        $libelleDroitDef = "<s>".$droitDef->getLibelle()."</s>";
                        $libelleDroitSpec = $droit->getTypeDroit()->getLibelle();
                    }
                    else
                    {
                        $libelleDroitDef = $droit->getTypeDroit()->getLibelle();
                        $libelleDroitSpec = "";
                    } 
                }
                $aaData[] = array(
                    "0" => $v->getIdUser(),
                    "1" => $userGuard->getFirstName() . " " . $userGuard->getLastName(),
                    "2" => $libelleDroitDef,
                    "3" => $libelleDroitSpec,
                    "4" => $actions,
                );
            }

            $output = array(
            "iTotalRecords" => count($pager),
            "iTotalDisplayRecords" => count($pager),
            "aaData" => $aaData,
            );

            return $this->renderText(json_encode($output));
        }
    }
}
